package wwl.lsf;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author wangwenliang
 * @Date 2021/10/11
 * @Description
 * @Lesson  break位置一定要准确了
 */
public class 矩阵置零 {


    static class Solution {

        public static void main(String[] args) {
            Solution solution = new Solution();

            int[][] a = {{1, 0}};
            solution.setZeroes(a);
            int[][] b = {{1, 0}};

            solution.setZeroesMySelf(b);

        }


        public void setZeroes(int[][] matrix) {
            int row = matrix.length;
            int col = matrix[0].length;
            boolean row0_flag = false;
            boolean col0_flag = false;
            // 第一行是否有零
            for (int j = 0; j < col; j++) {
                if (matrix[0][j] == 0) {
                    row0_flag = true;
                    break;
                }
            }
            // 第一列是否有零
            for (int i = 0; i < row; i++) {
                if (matrix[i][0] == 0) {
                    col0_flag = true;
                    break;
                }
            }
            // 把第一行第一列作为标志位
            for (int i = 1; i < row; i++) {
                for (int j = 1; j < col; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[i][0] = matrix[0][j] = 0;
                    }
                }
            }
            // 置0
            for (int i = 1; i < row; i++) {
                for (int j = 1; j < col; j++) {
                    if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                        matrix[i][j] = 0;
                    }
                }
            }
            if (row0_flag) {
                for (int j = 0; j < col; j++) {
                    matrix[0][j] = 0;
                }
            }
            if (col0_flag) {
                for (int i = 0; i < row; i++) {
                    matrix[i][0] = 0;
                }
            }
        }

        public void setZeroesMySelf(int[][] matrix) {

            int rows = matrix.length;

            int col = matrix[0].length;

            boolean row0Flag = false;
            boolean col0Flag = false;

            for (int j = 0; j < col; j++) {
                if (matrix[0][j] == 0) {
                    row0Flag = true;
                    break;
                }
            }
            for (int i = 0; i < rows; i++) {
                if (matrix[i][0] == 0) {
                    col0Flag = true;
                    break;
                }
            }
            for (int i = 1; i < rows; i++) {
                for (int j = 1; j < col; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[i][0] = matrix[0][j] = 0;
                    }
                }
            }

            for (int i = 1; i < rows; i++) {
                for (int j = 1; j < col; j++) {
                    if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                        matrix[i][j] = 0;
                    }
                }
            }

            if (row0Flag) {
                for (int i = 0; i < col; i++) {
                    matrix[0][i] = 0;
                }
            }
            if (col0Flag) {
                for (int i = 0; i < rows; i++) {
                    matrix[i][0] = 0;
                }
            }


        }
    }

}
